﻿cmake_minimum_required(VERSION 3.28)
project(asio_project)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置依赖路径
if (WIN32)
    set(DEPENDENCIES_ROOT "C:/code_dependencies")
    set(Boost_ROOT "${DEPENDENCIES_ROOT}/boost")
    set(Protobuf_ROOT "${DEPENDENCIES_ROOT}/protobuf")
    set(Protobuf_LIBRARY_DIR "${Protobuf_ROOT}/lib")
    set(nlohmann_json_ROOT "${DEPENDENCIES_ROOT}/nlohmann_json")
else ()
    message(FATAL_ERROR "Currently, only Windows is supported")
endif ()

# 查找依赖
find_package(Boost REQUIRED)
find_package(Protobuf REQUIRED)
find_package(nlohmann_json REQUIRED)

message(STATUS "Boost version: ${Boost_VERSION}")
message(STATUS "Protobuf version: ${Protobuf_VERSION}")
message(STATUS "nlohmann_json version: ${nlohmann_json_VERSION}")

# 处理 Protobuf 文件
file(GLOB PROTO_FILES "${CMAKE_CURRENT_SOURCE_DIR}/protos/*.proto")
if (PROTO_FILES)
    protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})
else ()
    message(WARNING "No .proto files found in ${CMAKE_CURRENT_SOURCE_DIR}/protos/")
endif ()

# 公共源文件（Node部分）
file(GLOB NODE_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/node/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/node/*.h")

# 定义一个函数来创建可执行文件
function(create_executable NAME SOURCES)
    add_executable(${NAME} ${SOURCES} ${NODE_SRCS} ${PROTO_SRCS} ${PROTO_HDRS})

    target_include_directories(${NAME} PRIVATE
            ${CMAKE_CURRENT_SOURCE_DIR}
            ${CMAKE_CURRENT_BINARY_DIR}
            ${Protobuf_INCLUDE_DIRS}
    )

    if (WIN32)
        target_compile_definitions(${NAME} PRIVATE _WIN32_WINNT=0x0A00)
    endif ()

    target_link_libraries(${NAME} PRIVATE
            Boost::boost
            nlohmann_json::nlohmann_json
            protobuf::libprotobuf
            ${Protobuf_LIBRARY_DIR}/abseil_dll.lib
    )
endfunction()

# 服务器可执行文件
file(GLOB_RECURSE SERVER_SRCS
        "${CMAKE_CURRENT_SOURCE_DIR}/server/*.cpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/server/*.h"
        "${CMAKE_CURRENT_SOURCE_DIR}/server/*.hpp"
)

create_executable(server "${SERVER_SRCS}")

# Simple Client 可执行文件
file(GLOB_RECURSE SIMPLE_CLIENT_SRCS
        "${CMAKE_CURRENT_SOURCE_DIR}/client/simple_client/*.cpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/client/simple_client/*.h"
        "${CMAKE_CURRENT_SOURCE_DIR}/client/simple_client/*.hpp"
)
create_executable(simple_client "${SIMPLE_CLIENT_SRCS}")

# Stress Client 可执行文件
file(GLOB_RECURSE STRESS_CLIENT_SRCS
        "${CMAKE_CURRENT_SOURCE_DIR}/client/stress_client/*.cpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/client/stress_client/*.h"
        "${CMAKE_CURRENT_SOURCE_DIR}/client/stress_client/*.hpp"
)
create_executable(stress_client "${STRESS_CLIENT_SRCS}")
